package com.exchange.controller;

import com.exchange.dto.GiftCardDto;
import com.exchange.entity.GiftCard;
import com.exchange.service.GiftCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/gift-cards")
@CrossOrigin(origins = "*")
@PreAuthorize("hasRole('MERCHANT') or hasRole('ADMIN')")
public class GiftCardController {
    
    @Autowired
    private GiftCardService giftCardService;
    
    @GetMapping
    public ResponseEntity<List<GiftCardDto>> getGiftCardsByMerchant() {
        // In a real implementation, we'd get the current merchant's ID from the security context
        // For now, using a placeholder
        Long currentMerchantId = 1L; // Placeholder - should come from security context
        List<GiftCardDto> giftCards = giftCardService.findByMerchantId(currentMerchantId);
        return ResponseEntity.ok(giftCards);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<GiftCardDto> getGiftCardById(@PathVariable Long id) {
        var result = giftCardService.findById(id);
        if (result.isPresent()) {
            return ResponseEntity.ok(result.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @PostMapping
    public ResponseEntity<GiftCardDto> createGiftCard(@RequestBody GiftCard giftCard) {
        // In a real implementation, we'd get the current merchant's ID from the security context
        // For now, using a placeholder
        Long currentMerchantId = 1L; // Placeholder - should come from security context
        GiftCardDto createdGiftCard = giftCardService.createGiftCard(giftCard, currentMerchantId);
        return ResponseEntity.ok(createdGiftCard);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<GiftCardDto> updateGiftCard(@PathVariable Long id, @RequestBody GiftCard giftCard) {
        GiftCardDto result = giftCardService.updateGiftCard(id, giftCard);
        if (result != null) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGiftCard(@PathVariable Long id) {
        boolean deleted = giftCardService.deleteGiftCard(id);
        return deleted ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }
    
    // Endpoint to validate a gift card
    @PostMapping("/validate")
    @PreAuthorize("permitAll")  // Allow any user to validate a gift card
    public ResponseEntity<?> validateGiftCard(@RequestBody ValidateGiftCardRequest request) {
        var result = giftCardService.validateGiftCard(request.getCardCode());
        if (result.isPresent()) {
            return ResponseEntity.ok(result.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    public static class ValidateGiftCardRequest {
        private String cardCode;
        
        public String getCardCode() { return cardCode; }
        public void setCardCode(String cardCode) { this.cardCode = cardCode; }
    }
}